package com.hisysteTopet.hisystepet.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hisysteTopet.hisystepet.bean.*;
import com.hisysteTopet.hisystepet.interceptors.JWTInterceptor;
import com.hisysteTopet.hisystepet.mapper.*;
import com.hisysteTopet.hisystepet.service.*;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class AdminMessageController {

    @Autowired
    private Msg msg;

    @Autowired
    private UserMessage userMessage;

    @Autowired
    UserMessageMapper userMessageMapper;

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private Foster foster;

    @Autowired
    private FosterMapper fosterMapper;

    @Autowired
    private FosterService fosterService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetService petService;

    @Autowired
    private PetLifeGoodsService petLifeGoodsService;

    @Autowired
    private IndentToPetMapper indentToPetMapper;

    @Autowired
    private IndentToPetLifeGoodsMapper indentToPetLifeGoodsMapper;



    // 接收宠物上传
    @RequestMapping(value = "uploadPet" , method = RequestMethod.POST)
    @ResponseBody
    public Msg uploadPet( String petName,
                         String variety,
                         String introduce,
                         Integer price,
                         Integer petNum,
                         MultipartFile headerImg,
                            HttpServletRequest request
                         ) throws IOException {

        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            String petNameMessage = petName;
            String varietyMessage = variety;
            String introduceMessage = introduce;
            petNameMessage = petNameMessage.substring(petNameMessage.indexOf(",")+1);
            varietyMessage = varietyMessage.substring(varietyMessage.indexOf(",")+1);
            introduceMessage = introduceMessage.substring(introduceMessage.indexOf(",")+1);
            if(!headerImg.isEmpty()){
                //保存到本地
                // 获取源文件名字
                String originalFilename = headerImg.getOriginalFilename();
                String  txt = originalFilename.substring(originalFilename.indexOf(".")+1);
                if (txt.equals("png") || txt.equals("jpg") || txt.equals("webp")){
                    File directory = new File("");// 参数为空
                    // 获取服务器再文件系统中的真实历经
//            String serverRootPath = request.getServletContext().getContextPath("/");
                    String courseFile = directory.getCanonicalPath()+"\\src\\main\\resources\\static\\img\\";
                    headerImg.transferTo(new File(courseFile+originalFilename));
                    String url = "img\\" + originalFilename;
                    petService.insertPet(petNameMessage,varietyMessage,
                            url,introduceMessage,price,petNum);
                    msg = Msg.success();
                }else {
                    msg = Msg.fail();
                }

            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 接收宠物生活用品上传
    @RequestMapping(value = "uploadLifeGoods" , method = RequestMethod.POST)
    @ResponseBody
    public Msg uploadLifeGoods( String priceName,
                       String introduce,
                       Integer price,
                       Integer priceNum,
                       MultipartFile headerImg,
                       HttpServletRequest request
    ) throws IOException {

        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            String petNameMessage = priceName;

            String introduceMessage = introduce;
            petNameMessage = petNameMessage.substring(petNameMessage.indexOf(",")+1);
            introduceMessage = introduceMessage.substring(introduceMessage.indexOf(",")+1);
            if(!headerImg.isEmpty()){
                //保存到本地
                // 获取源文件名字
                String originalFilename = headerImg.getOriginalFilename();
                String  txt = originalFilename.substring(originalFilename.indexOf(".")+1);
                if (txt.equals("png") || txt.equals("jpg") || txt.equals("webp")){
                    File directory = new File("");// 参数为空
                    // 获取服务器再文件系统中的真实历经
//            String serverRootPath = request.getServletContext().getContextPath("/");
                    String courseFile = directory.getCanonicalPath()+"\\src\\main\\resources\\static\\img\\";
                    headerImg.transferTo(new File(courseFile+originalFilename));
                    String url = "img\\" + originalFilename;
                    petLifeGoodsService.insertLifeGoods(petNameMessage,url,introduceMessage,price,priceNum);
                    msg = Msg.success();
                }else {
                    msg = Msg.fail();
                }

            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return  msg;
    }


    // 查询所有的用户信息
    @RequestMapping(value = "findAllToUserMessage")
    @ResponseBody
    public Msg findAllToUserMessage(int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            Page page = new Page(currentPage,10);
            msg = Msg.success().add("userMessageList",
                    userMessageMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 查询所有的公告
    @RequestMapping("/noticeAllMessage")
    @ResponseBody
    public Msg allNoticeMessage(){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            msg = Msg.success().add("messageList", noticeService.selectListNotice());
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 查询所有的寄养信息
    @RequestMapping("/allFosterMessage")
    @ResponseBody
    public Msg allFosterMessage(int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            Page page = new Page(currentPage,10);
            QueryWrapper<Foster> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("audit","待审核");
            msg = Msg.success().add("fosterList",
                    fosterMapper.selectPage(page,queryWrapper));
        }else {
            msg = Msg.fail();
        }
        return msg;
    }

    // 查询所有的宠物库存
    @RequestMapping("/petManagement")
    @ResponseBody
    public Msg petManagement(int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            Page page = new Page(currentPage,6);
            msg = Msg.success().add("petManagement",
                    petMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 查询所有的宠物订单信息
    @RequestMapping("/petAllIndentMessage")
    @ResponseBody
    public Msg petAllIndentMessage(int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            Page page = new Page(currentPage, 10);
            msg = Msg.success().add("petAllIndentMessage",
                    indentToPetMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }

        return msg;
    }


    // 查询所有的宠物生活用品订单信息
    @RequestMapping("/lifeGoodsAllIndentMessage")
    @ResponseBody
    public Msg lifeGoodsAllIndentMessage(int currentPage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            Page page = new Page(currentPage,10);
            msg = Msg.success().add("lifeGoodsAllIndentMessage",
                    indentToPetLifeGoodsMapper.selectPage(page,null));
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 修改公告信息
    @RequestMapping(value = "/updateNoticeMessage",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateNoticeMessage(Integer noticeId,String noticeMessage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (noticeId != null && !noticeMessage.equals(" ")){
                noticeService.updateNoticeMessage(noticeMessage,noticeId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 删除公告信息
    @RequestMapping(value = "/updateNotice" , method = RequestMethod.DELETE)
    @ResponseBody
    public Msg updateNotice(Integer noticeId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (noticeId != null){
                noticeService.deleteNoticeMessage(noticeId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 添加新的公告信息
    @RequestMapping(value = "/insertNotice" , method = RequestMethod.POST)
    @ResponseBody
    public Msg insertNotice(String noticeMessage){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (!noticeMessage.equals(" ")){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date data = Timestamp.valueOf(df.format(new Date()));// new Date()为获取当前系统时间
                noticeService.insertNoticeMessage(noticeMessage,data);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 修改宠物的信息
    @RequestMapping(value = "updatePetMessage" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updatePetMessage(Pet pet){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (pet != null){
                if (pet.getPetName().equals("")){
                    pet.setPetName(null);
                }
                if (pet.getPetVariety().equals("")){
                    pet.setPetVariety(null);
                }
                if (pet.getPetIntroduce().equals("")){
                    pet.setPetIntroduce(null);
                }
                petService.updatePetMessage(pet);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

                return msg;
    }

    // 删除宠物
    @RequestMapping(value = "deletePetMessage" , method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deletePetMessage(String petId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (petId != null){
                petService.deletePetMessageById(petId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 修改宠物生活用品的信息
    @RequestMapping(value = "updatePetLifeGoodsMessage" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updatePetLifeGoodsMessage(PetLifeGoods petLifeGoods){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (petLifeGoods != null){
                if (petLifeGoods.getPriceName().equals("")){
                    petLifeGoods.setPriceName(null);
                }

                if (petLifeGoods.getIntroduce().equals("")){
                    petLifeGoods.setIntroduce(null);
                }
                petLifeGoodsService.updatePetLifeGoodsMessage(petLifeGoods);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 删除宠物
    @RequestMapping(value = "deletePetLifeGoodsMessage" , method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deletePetLifeGoodsMessageById(String priceId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (priceId != null){
                petLifeGoodsService.deletePetLifeGoodsMessageById(priceId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 寄养审核通过
    @RequestMapping(value = "updateFosterMessageByYes" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateFosterMessageByYes(Integer fosterId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (fosterId != null){
                String audit = "已审核";
                String auditResults = "通过";
                fosterService.updateFosterMessageByYes(audit,auditResults,fosterId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 寄养审核不通过
    @RequestMapping(value = "updateFosterMessageByNo" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateFosterMessageByNo(Integer fosterId){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (fosterId != null){
                String audit = "已审核";
                String auditResults = "不通过";
                fosterService.updateFosterMessageByNo(audit,auditResults,fosterId);
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 修改宠物的库存
    @RequestMapping(value = "updatePetManagement" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updatePetManagement(Pet pet){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            if (pet.getPetId() != null && pet.getPetNum() != null){
                petService.updateToPetNumByAdmin(pet.getPetNum(),pet.getPetId());
                msg = Msg.success();
            }else {
                msg = Msg.fail();
            }
        }else {
            msg = Msg.fail();
        }

        return msg;
    }

    // 修改宠物审过用品的库存
    @RequestMapping(value = "updateLifeGoodsManagement" , method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateLifeGoodsManagement(PetLifeGoods petLifeGoods){
        String username = JWTInterceptor.username;
        Boolean tokenState = JWTInterceptor.tokenState;
        if (tokenState && username != null) {
            petLifeGoodsService.updateToPriceNum(petLifeGoods.getPriceNum(),
                    petLifeGoods.getPriceId());
            msg = Msg.success();
        }else {
            msg = Msg.fail();
        }

        return msg;
    }


}
